package com.timmy.google_play.holder;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.timmy.google_play.R;
import com.timmy.google_play.base.BaseHolder;
import com.timmy.google_play.base.MyApplication;
import com.timmy.google_play.conf.Constants;
import com.timmy.google_play.utils.UIUtils;
import com.timmy.google_play.views.ChildViewPager;

import java.util.List;

import butterknife.BindView;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by Timmy on 2016/12/12.
 * 首页轮播图所对应的holder
 * 1.提供视图
 * 2.接收/加载数据
 * 3.数据和视图的绑定
 */
public class HomePictureHolder extends BaseHolder<List<String>> {
    @BindView(R.id.item_home_picture_pager)
    ChildViewPager mItemHomePicturePager;

    @BindView(R.id.item_home_picture_container_indicator)
    LinearLayout mItemHomePictureContainerIndicator;
    private List<String>  mPictureData;
    private AutoScollTask mAutoScollTask;

    /**
     * 决定提供的视图是什么样子
     *
     * @return
     */
    @Override
    protected View initHolderVIew() {
        return View.inflate(UIUtils.getContext(), R.layout.item_home_picture, null);
    }

    /**
     * 数据和视图的绑定
     */
    @Override
    protected void refreshHolderView(List<String> data) {
        //保存数据集到成员变
        mPictureData = data;

        mItemHomePicturePager.setAdapter(new HomePicturePagerAdapter());

        //绑定mItemHomePictureContainerIndicator 数据
        for (int i = 0; i < mPictureData.size(); i++) {

            ImageView ivIndicator = new ImageView(UIUtils.getContext());

            //设置indicator 背景
            ivIndicator.setBackgroundResource(R.drawable.indicator_normal);

            //默认选中第0个
            if (i == 0) {
                ivIndicator.setBackgroundResource(R.drawable.indicator_selected);
            }

            int widht  = LinearLayout.LayoutParams.WRAP_CONTENT;
            int height = LinearLayout.LayoutParams.WRAP_CONTENT;

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(widht, height);

            params.leftMargin = UIUtils.dp2px(3);  //px -- dp
            params.bottomMargin = UIUtils.dp2px(10);

            mItemHomePictureContainerIndicator.addView(ivIndicator, params);
        }

        //监听轮播图的切换,同时切换indicator
        mItemHomePicturePager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //处理position 越界问题
                position = position % mPictureData.size();

                //选中的时候切换indicator
                //1.还原所有的
                for (int i = 0; i < mPictureData.size(); i++) {
                    ImageView iv = (ImageView) mItemHomePictureContainerIndicator.getChildAt(i);

                    iv.setBackgroundResource(R.drawable.indicator_normal);

                    //2. 选中应该选中的
                    if (i == position) {
                        iv.setBackgroundResource(R.drawable.indicator_selected);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //设置当前viewPager 为getCount 中间值
        int diff = Integer.MAX_VALUE / 2 % mPictureData.size();
        int item = Integer.MAX_VALUE / 2;
        mItemHomePicturePager.setCurrentItem(item - diff);      //在这里.加或者减都可以

        //自动轮播实现
        mAutoScollTask = new AutoScollTask();
        mAutoScollTask.start();

        //按下去的时候停止滚动,设置点击事件
        mItemHomePicturePager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:           //按下的时候停止轮播
                        mAutoScollTask.stop();
                        break;
                    case MotionEvent.ACTION_UP:             //抬起的时候开始轮播
                        mAutoScollTask.start();
                        break;
                }

                return false;
            }
        });


    }

    class AutoScollTask implements Runnable {

        //开始任务
        public void start() {
            MyApplication.getHandler().postDelayed(this, 2500);
        }

        //移除当前任务
        public void stop() {
            MyApplication.getHandler().removeCallbacks(this);
        }

        @Override
        public void run() {
            //进行viewpager 轮播

            int currentItem = mItemHomePicturePager.getCurrentItem();
            currentItem++;
            mItemHomePicturePager.setCurrentItem(currentItem);

            start();

        }
    }

    class HomePicturePagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            if (mPictureData != null) {
                //                return mPictureData.size();
                return Integer.MAX_VALUE;
            }
            return 0;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //处理position 越界问题
            position = position % mPictureData.size();

            String data = mPictureData.get(position);

            ImageView iv = new ImageView(UIUtils.getContext());

            iv.setScaleType(ImageView.ScaleType.FIT_XY);


            Picasso.with(UIUtils.getContext()).load(Constants.URLS.IMAGEBASEURL + data).into(iv);

            container.addView(iv);

            return iv;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
